//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CostStructureItemCategoryAction.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:26:34  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.action;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.facade.CostStructureFacade;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc05Itemcatgry;
import ar.com.ford.it.fullEconomics.view.form.CostStructureItemCategoryForm;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.struts.AbstractBaseAction;
import ar.com.ford.it.webengine.struts.FordActionMapping;
import ar.com.ford.it.webengine.websession.WebSession;

public class CostStructureItemCategoryAction extends AbstractBaseAction {

    /**
     * Setea el mensaje de "busqueda sin resultados" cuando la busqueda no trae
     * resultados
     * 
     * @param request
     * Request donde se guarda el mensaje
     */
    private void setMessageSearchWithNoResults(HttpServletRequest request) {
        ActionMessages am = new ActionMessages();
        MessageResources messageResources = (MessageResources) getResources(request);

        am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                "customMessage", messageResources
                        .getMessage("message.noResults")));
        saveMessages(request, am);
    }

    /**
     * Setea el mensaje de Item Category repetido
     * 
     * @param request
     */
    private void setErrorItemCategoryAlreadyExists(HttpServletRequest request) {
        ActionMessages am = new ActionMessages();
        MessageResources messageResources = (MessageResources) getResources(request);

        am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                "error.duplicateItem.m", messageResources
                        .getMessage("costStructure.itemCategory")));
        this.saveErrors(request, am);
    }

    /**
     * Setea el mensaje que se refiere a que el Item Category no puede ser
     * borrado dado que hay Items que hacen referencia a el
     * 
     * @param request
     */
    private void setErrorItemCategoryHasReferences(HttpServletRequest request) {
        ActionMessages am = new ActionMessages();
        MessageResources messageResources = (MessageResources) getResources(request);

        am
                .add(
                        ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage(
                                "customMessage",
                                messageResources
                                        .getMessage("costStructure.itemCategory.error.hasReferences")));
        saveErrors(request, am);
    }

    /**
     * Resetea el formulario y envia a la pagina principal de Cost Structure
     * Item Categories
     * 
     */
    public ActionForward initPage(FordActionMapping mapping,
                                  ActionForm actionForm,
                                  HttpServletRequest request,
                                  HttpServletResponse response,
                                  WebSession webSession, ActionMessages msgs,
                                  ActionMessages warns, ActionMessages errorMsgs) {
        CostStructureItemCategoryForm form = (CostStructureItemCategoryForm) actionForm;

        form.resetForm();
        return mapping.findForward("success");
    }

    /**
     * 
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward findCostStructureItemCategory(
                                                       FordActionMapping mapping,
                                                       ActionForm actionForm,
                                                       HttpServletRequest request,
                                                       HttpServletResponse response,
                                                       WebSession webSession,
                                                       ActionMessages msgs,
                                                       ActionMessages warns,
                                                       ActionMessages errorMsgs) {

        CostStructureItemCategoryForm form = (CostStructureItemCategoryForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CostStructureFacade facade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);
        Collection<Teesc05Itemcatgry> res;
        try {
            res = facade.findItemCategories(form.getCodeFind(), form
                    .getNameFind(), user.getTeesg01Plant());

            if (res.isEmpty()) {
                setMessageSearchWithNoResults(request);
            }
            form.setItemCategories(res);
            form.clearCreateUpdateFields();
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        return mapping.findForward("success");
    }

    /**
     * Setea los campos de edicion de Cost Structure Item Cateories con los
     * valores del Item Category seleccionado mediante el listado
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward editCostStructureItemCategory(
                                                       FordActionMapping mapping,
                                                       ActionForm actionForm,
                                                       HttpServletRequest request,
                                                       HttpServletResponse response,
                                                       WebSession webSession,
                                                       ActionMessages msgs,
                                                       ActionMessages warns,
                                                       ActionMessages errorMsgs) {
        CostStructureItemCategoryForm form = (CostStructureItemCategoryForm) actionForm;

        form.setItemCategoryForEdit(form.getItemCategorySelected());
        return mapping.findForward("success");
    }

    /**
     * En caso de que se haya seleccionado algun Item Category del listado,
     * llama al metodo encargado de crear el Item Category
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward createCostStructureItemCategory(
                                                         FordActionMapping mapping,
                                                         ActionForm actionForm,
                                                         HttpServletRequest request,
                                                         HttpServletResponse response,
                                                         WebSession webSession,
                                                         ActionMessages msgs,
                                                         ActionMessages warns,
                                                         ActionMessages errorMsgs) {

        CostStructureItemCategoryForm form = (CostStructureItemCategoryForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CostStructureFacade facade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);
        try {
            if (!facade.itemCategoryNameAlreadyExists(form.getName(), user
                    .getTeesg01Plant())) {
                facade
                        .createItemCategory(form.getItemCategoryForCreate(),
                                user);
            } else {
                setErrorItemCategoryAlreadyExists(request);
            }
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        return mapping.findForward("success");
    }

    /**
     * En caso de que se haya seleccionado algun Item Category del listado,
     * llama al metodo encargado de actualizar el Item Category
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward updateCostStructureItemCategory(
                                                         FordActionMapping mapping,
                                                         ActionForm actionForm,
                                                         HttpServletRequest request,
                                                         HttpServletResponse response,
                                                         WebSession webSession,
                                                         ActionMessages msgs,
                                                         ActionMessages warns,
                                                         ActionMessages errorMsgs) {

        CostStructureItemCategoryForm form = (CostStructureItemCategoryForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CostStructureFacade facade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);

        try {
            if (!facade.itemCategoryNameAlreadyExists(form.getName(), user
                    .getTeesg01Plant())) {
                facade.updateItemCategory(form.getItemCategoryForUpdate(form
                        .getItemCategorySelected()), user);
            } else {
                setErrorItemCategoryAlreadyExists(request);
            }
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        return mapping.findForward("success");
    }

    /**
     * Elimina el Item Category seleccionado
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward deleteCostStructureItemCategory(
                                                         FordActionMapping mapping,
                                                         ActionForm actionForm,
                                                         HttpServletRequest request,
                                                         HttpServletResponse response,
                                                         WebSession webSession,
                                                         ActionMessages msgs,
                                                         ActionMessages warns,
                                                         ActionMessages errorMsgs) {
        CostStructureItemCategoryForm form = (CostStructureItemCategoryForm) actionForm;
        CostStructureFacade facade = (CostStructureFacade) FacadeProvider
                .getFacade(request, CostStructureFacade.class);

        try {
            if (facade.itemCategoryCanBeDeleted(form.getItemCategorySelected())) {
                facade.deleteItemCategory(form.getItemCategorySelected());
            } else {
                this.setErrorItemCategoryHasReferences(request);
            }
        } catch (FullEconomicsException e) {
            ActionMessages am = new ActionMessages();
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e
                    .getMessageResource()));
            saveErrors(request, am);
        }
        return mapping.findForward("success");
    }
    
    /**
     * Setea los campos de edicion de Cost Structure Item Cateories con los
     * valores del Item Category seleccionado mediante el listado
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward cleanCostStructureItemCategory(
                                                       FordActionMapping mapping,
                                                       ActionForm actionForm,
                                                       HttpServletRequest request,
                                                       HttpServletResponse response,
                                                       WebSession webSession,
                                                       ActionMessages msgs,
                                                       ActionMessages warns,
                                                       ActionMessages errorMsgs) {
        CostStructureItemCategoryForm form = (CostStructureItemCategoryForm) actionForm;

        form.clearCreateUpdateFields();
        return mapping.findForward("success");
    }
    
    /**
     * Setea los campos de edicion de Cost Structure Item Cateories con los
     * valores del Item Category seleccionado mediante el listado
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward cleanFindCostStructureItemCategory(
                                                       FordActionMapping mapping,
                                                       ActionForm actionForm,
                                                       HttpServletRequest request,
                                                       HttpServletResponse response,
                                                       WebSession webSession,
                                                       ActionMessages msgs,
                                                       ActionMessages warns,
                                                       ActionMessages errorMsgs) {
        CostStructureItemCategoryForm form = (CostStructureItemCategoryForm) actionForm;

        form.cleanFindFields();
        return mapping.findForward("success");
    }    

}
